package com.bh.ly.oa.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bh.ly.oa.GlobalData;
import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.MainAdapter;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListParser;

public class ItemActivity extends Activity {

	
	NSArray items;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item);
		
		ImageButton button = (ImageButton) findViewById(R.id.back_button);
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});
		
		int index = getIntent().getIntExtra("index", 0);
		NSArray menuListArray = null;

		try {
			if (GlobalData.getInstance().getMenuListArray() != null) {
				menuListArray = GlobalData.getInstance().getMenuListArray();
			} else {
			    menuListArray = (NSArray) PropertyListParser.parse(this.getAssets().open("menuData.plist"));
			    GlobalData.getInstance().setMenuListArray(menuListArray);
			}
		} catch (IOException e) {

			Toast.makeText(this, "open assets erro:" + e.getMessage(), 30).show();
			e.printStackTrace();
		} catch (Exception e) {

			Toast.makeText(this, "has erro:" + e.getMessage(), 30).show();
			e.printStackTrace();
		}

		NSDictionary dictionary = (NSDictionary) menuListArray.objectAtIndex(index);
		((TextView) findViewById(R.id.title_textview)).setText(((NSString)dictionary.objectForKey("menuname")).getContent());

		ListView listView = ((ListView) findViewById(R.id.item_list));
		items =  (NSArray) dictionary.objectForKey("submenes");
		listView.setAdapter(new MainAdapter(this, items));
		listView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				NSDictionary dictionary = (NSDictionary)items.objectAtIndex(arg2);
				
				String url  = ((NSString)dictionary.objectForKey("submenuurl")).getContent();
				String name = ((NSString)dictionary.objectForKey("submenuname")).getContent();		
				Intent intent = null;
				switch (Integer.parseInt(url)) {
				case 1:
					intent= new Intent(ItemActivity.this, OrderViewActivity.class);
					break;
				case 2:
					intent= new Intent(ItemActivity.this, RelbackOrderViewActivity.class);
					break;
//				case 3:
//					intent= new Intent(ItemActivity.this, TestActivity.class);
//					break;
				case 4:
					intent= new Intent(ItemActivity.this,GoodsSaleDeptDtlActivity.class);//TODO 这里URL是一个网络地址，不是数字。所以有异常
					break;
				case 5:
					intent= new Intent(ItemActivity.this,SupplierSaleDailyActiivity.class);
					break;
				case 6:
					intent= new Intent(ItemActivity.this,NoticeActiivity.class);
					break;
				case 7:
					intent= new Intent(ItemActivity.this,LicenseActiivity.class);
					break;
				default:
					Toast.makeText(ItemActivity.this, "敬请期待....", Toast.LENGTH_SHORT).show();
					return;
				}
				intent.putExtra("title", name);
				startActivity(intent);
			}
		});
	}
}
